package com.my.study.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/6/14 13:21
 **/
@Slf4j(topic = "c.TestAQS")
public class TestAQS {

    public static void main(String[] args) {
//        MyLock myLock = new MyLock();
//
//        new Thread(() -> {
//            myLock.lock();
//            try {
//                log.debug("t1 lock...");
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } finally {
//                log.debug("t1 unlock...");
//                myLock.unlock();
//            }
//
//        }, "t1").start();
//
//        new Thread(() -> {
//            myLock.lock();
//            try {
//                log.debug("t2 lock...");
//            } finally {
//                log.debug("t2 unlock...");
//                myLock.unlock();
//            }
//
//        }, "t2").start();
        
        testSynchronousQueue();
    }
    
    private static void testSynchronousQueue() {
        SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "\t 入队列 1");
                synchronousQueue.put("1");
                System.out.println(Thread.currentThread().getName() + "\t 入队列 2");
                synchronousQueue.put("2");
                System.out.println(Thread.currentThread().getName() + "\t 入队列 3");
                synchronousQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "AAAAA").start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName() + "\t 出队列 " + synchronousQueue.take());
                TimeUnit.SECONDS.sleep(5);
                System.out.println(Thread.currentThread().getName() + "\t 出队列 " + synchronousQueue.take());
//                TimeUnit.SECONDS.sleep(5);
//                System.out.println(Thread.currentThread().getName() + "\t 出队列 " + synchronousQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "BBBBB").start();

    }
}
